package de.tum.in.eist.poll.server;

import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.tum.in.eist.poll.client.AdminService;
import de.tum.in.eist.poll.shared.Lecturer;
import de.tum.in.eist.poll.shared.Poll;
import de.tum.in.eist.poll.shared.Student;
import de.tum.in.eist.poll.shared.exceptions.DuplicatePollException;
import de.tum.in.eist.poll.shared.exceptions.DuplicateUserException;
import de.tum.in.eist.poll.shared.exceptions.InvalidAdminLoginException;

@SuppressWarnings("serial")
public class AdminServiceImpl extends RemoteServiceServlet implements
		AdminService {
	/**
	 * 
	 * Validates the admin login when called
	 * 
	 * @param l
	 *            lecturer login which will be validated
	 * @return wheter the lecturer is legit or not
	 */
	public boolean authAdmin(Lecturer l) {
		return Database.userDatabase.contains(l);
	}

	/**
	 * Adds a user to the database
	 * 
	 * @param lecturer
	 *            will be validated with the database
	 * @param studentID
	 *            the student id to be added
	 * 
	 * @throws InvalidAdminLoginException
	 *             if the admin login could not be validated
	 * 
	 * @throws DuplicateUserException
	 *             if a user with the same identifier already exists in the
	 *             database
	 * 
	 */
	@Override
	public void addUser(Lecturer lecturer, String studentID)
			throws DuplicateUserException, InvalidAdminLoginException {
		if (!authAdmin(lecturer))
			throw new InvalidAdminLoginException();
		Database.userDatabase.addUser(new Student(studentID));
	}

	/**
	 * Returns a list of all the Student IDs in the database
	 * 
	 * @param lecturer
	 *            will be validated with the database
	 * 
	 * @return list of Student IDs
	 * 
	 * @throws InvalidAdminLoginException
	 *             if the admin login could not be validated
	 */

	@Override
	public List<String> getUserMatrikelList(Lecturer lecturer)
			throws InvalidAdminLoginException {
		if (!authAdmin(lecturer))
			throw new InvalidAdminLoginException();
		return Database.userDatabase.getStudentIDList();
	}

	// TODO: Implement whitelist box
	// @Override
	// public void parseUserList(Lecturer l, String userList, String sep) throws
	// DuplicateUserException, InvalidAdminLoginException{
	// if(!authAdmin(l)) throw new InvalidAdminLoginException();
	// List<String> newUserMatrikelList = Arrays.asList(userList.split(sep));
	// List<String> oldUserMatrikelList = udm.getStudentIDList();
	// newUserMatrikelList.removeAll(oldUserMatrikelList);
	// oldUserMatrikelList.removeAll(newUserMatrikelList);
	// //Add the new Users
	// for(String matrikel: newUserMatrikelList ){
	// udm.addUser(new Student(matrikel));
	// }
	// //Remove the deleted Users
	// for(String matrikel: oldUserMatrikelList){
	// udm.deleteUser(matrikel);
	// }
	// }

	/**
	 * 
	 * Delegates a new poll to the datastore managers
	 * 
	 * @param l
	 *            will be validated with the database
	 * 
	 * @param poll
	 *            the poll to be put into the database
	 * 
	 * @throws DuplicatePollException
	 *             if the a poll with the same identifier already exists in the
	 *             database
	 * @throws InvalidAdminLoginException
	 *             if the admin login could not be validated
	 */
	@Override
	public void addPoll(Lecturer l, Poll poll) throws DuplicatePollException,
			InvalidAdminLoginException {
		if (!authAdmin(l))
			throw new InvalidAdminLoginException();
		Database.pollDatabase.addPoll(poll);
	}
}
